// Copyright  2012 Petr TALLA. [petr.talla@gmail.com]
// This file is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This file is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY;  without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
// [http://www.gnu.org/copyleft/lesser.html]
//=============================================================================

package tc.crase.app;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JToolBar;

import tc.common.utility.TcCmd;

import tc.crase.cmds.CmdAdd;
import tc.crase.cmds.CmdCreate;
import tc.crase.cmds.CmdDelete;
import tc.crase.cmds.CmdDocs;
import tc.crase.cmds.CmdEdit;
import tc.crase.cmds.CmdLink;
import tc.crase.cmds.CmdList;
import tc.crase.cmds.CmdLoadSave;
import tc.crase.cmds.CmdMerge;
import tc.crase.cmds.CmdMove;
import tc.crase.cmds.CmdRemove;
import tc.crase.cmds.CmdUtility;
import tc.crase.cmds.CmdViewMove;
import tc.crase.cmds.CmdViewZoom;
import tc.crase.view.ViewRoot;

public class Toolbar extends JToolBar implements ActionListener {
	private Color background = new Color(255, 255, 255);
	private Font font;
	
	public Toolbar() {
		JButton loadsaveButton = null;
		try {
			ImageIcon icon = new ImageIcon(getClass().getResource("images/file.png"));
			loadsaveButton = new JButton("File ", icon);
		}
		catch (Exception e) {
			loadsaveButton = new JButton(" File ");
		}
		loadsaveButton.setActionCommand("loadsave"); 
		loadsaveButton.addActionListener(this);
		loadsaveButton.setToolTipText("Load/Save");
		
		background = loadsaveButton.getBackground();
		font = loadsaveButton.getFont();
		TcCmd.setActiveCommand(new CmdLoadSave());
		mark(loadsaveButton);
		
		JButton button_utility = null;
		try {
			ImageIcon icon = new ImageIcon(getClass().getResource("images/utility.png"));
			button_utility = new JButton("Utility", icon);
		}
		catch (Exception e) {
			button_utility = new JButton(" Utility ");
		}
		button_utility.setActionCommand("utility");
		button_utility.addActionListener(this);
		button_utility.setToolTipText("Utility opperations.");
		
		JButton button_list = null;
		try {
			ImageIcon icon = new ImageIcon(getClass().getResource("images/list.png"));
			button_list = new JButton("List ", icon);
		}
		catch (Exception e) {
			button_list = new JButton(" List ");
		}
		button_list.setActionCommand("list");
		button_list.addActionListener(this);
		button_list.setToolTipText("Open object list.");
		
		JButton button_create = null;
		try {
			ImageIcon icon = new ImageIcon(getClass().getResource("images/create.png"));
			button_create = new JButton(icon);
		}
		catch (Exception e) {
			button_create = new JButton(" Create ");
		}
		button_create.setActionCommand("create");
		button_create.addActionListener(this);
		button_create.setToolTipText("Create object.");
		
		JButton button_edit = null;
		try {
			ImageIcon icon = new ImageIcon(getClass().getResource("images/edit.png"));
			button_edit = new JButton(icon);
		}
		catch (Exception e) {
			button_edit = new JButton(" Edit ");
		}
		button_edit.setActionCommand("edit");
		button_edit.addActionListener(this);
		button_edit.setToolTipText("Edit object.");
		
		JButton button_link = null;
		try {
			ImageIcon icon = new ImageIcon(getClass().getResource("images/link.png"));
			button_link = new JButton(icon);
		}
		catch (Exception e) {
			button_link = new JButton(" Link ");
		}
		button_link.setActionCommand("link");
		button_link.addActionListener(this);
		button_link.setToolTipText("Create objects link.");
		
		JButton button_merge = null;
		try {
			ImageIcon icon = new ImageIcon(getClass().getResource("images/merge.png"));
			button_merge = new JButton(icon);
		}
		catch (Exception e) {
			button_merge = new JButton(" Merge ");
		}
		button_merge.setActionCommand("merge");
		button_merge.addActionListener(this);
		button_merge.setToolTipText("Merge objects.");
		
		JButton button_delete = null;
		try {
			ImageIcon icon = new ImageIcon(getClass().getResource("images/delete.png"));
			button_delete = new JButton(icon);
		}
		catch (Exception e) {
			button_delete = new JButton(" Delete ");
		}
		button_delete.setActionCommand("delete");
		button_delete.addActionListener(this);
		button_delete.setToolTipText("Delete object.");
				
		JButton button_docs = null;
		try {
			ImageIcon icon = new ImageIcon(getClass().getResource("images/docs.png"));
			button_docs = new JButton("Graph ", icon);
		}
		catch (Exception e) {
			button_docs = new JButton(" Graph ");
		}
		button_docs.setActionCommand("docs");
		button_docs.addActionListener(this);
		button_docs.setToolTipText("View graph.");
				
		JButton button_add = null;
		try {
			ImageIcon icon = new ImageIcon(getClass().getResource("images/add.png"));
			button_add = new JButton(icon);
		}
		catch (Exception e) {
			button_add = new JButton(" Add ");
		}
		button_add.setActionCommand("add");
		button_add.addActionListener(this);
		button_add.setToolTipText("Add object in graph.");
		
		JButton button_move = null;
		try {
			ImageIcon icon = new ImageIcon(getClass().getResource("images/move.png"));
			button_move = new JButton(icon);
		}
		catch (Exception e) {
			button_move = new JButton(" Move ");
		}
		button_move.setActionCommand("move");
		button_move.addActionListener(this);
		button_move.setToolTipText("Move object in graph.");
		
		JButton button_remove = null;
		try {
			ImageIcon icon = new ImageIcon(getClass().getResource("images/remove.png"));
			button_remove = new JButton(icon);
		}
		catch (Exception e) {
			button_remove = new JButton(" Remove ");
		}
		button_remove.setActionCommand("remove");
		button_remove.addActionListener(this);
		button_remove.setToolTipText("Remove object from graph.");
		
		JButton button_viewMove = null;
		try {
			ImageIcon icon = new ImageIcon(getClass().getResource("images/viewMove.png"));
			button_viewMove = new JButton(icon);
		}
		catch (Exception e) {
			button_viewMove = new JButton(" View Move ");
		}
		button_viewMove.setActionCommand("viewMove");
		button_viewMove.addActionListener(this);
		button_viewMove.setToolTipText("Move view.");
		
		JButton button_viewZoom = null;
		try {
			ImageIcon icon = new ImageIcon(getClass().getResource("images/viewZoom.png"));
			button_viewZoom = new JButton(icon);
		}
		catch (Exception e) {
			button_viewZoom = new JButton(" View Zoom ");
		}
		button_viewZoom.setActionCommand("viewZoom");
		button_viewZoom.addActionListener(this);
		button_viewZoom.setToolTipText("Zoom view.");

		
		add(new JToolBar.Separator(new Dimension(0,35)));
		add(loadsaveButton);
		add(button_utility);
		add(button_list);
		add(new JToolBar.Separator(new Dimension(40,0)));
		add(button_create);
		add(new JToolBar.Separator(new Dimension(15,0)));
		add(button_edit);
		add(button_link);
		add(button_merge);
		add(button_delete);
		add(new JToolBar.Separator(new Dimension(40,0)));
		add(button_docs);
		add(button_add);
		add(button_move);
		add(button_remove);
		add(new JToolBar.Separator(new Dimension(15,0)));
		add(button_viewMove);
		add(button_viewZoom);
		add(new JToolBar.Separator(new Dimension(50,0)));
	}
	
	public void actionPerformed(ActionEvent e) {
		if ("loadsave".equals(e.getActionCommand())) {
			TcCmd.setActiveCommand(new CmdLoadSave());
		}
		else if ("utility".equals(e.getActionCommand())) {
			TcCmd.setActiveCommand(new CmdUtility());
		}
		else if ("list".equals(e.getActionCommand())) {
			TcCmd.setActiveCommand(new CmdList());
		}
		else if ("docs".equals(e.getActionCommand())) {
			TcCmd.setActiveCommand(new CmdDocs());
		}
		else if ("create".equals(e.getActionCommand())) {
			TcCmd.setActiveCommand(new CmdCreate());
		}
		else if ("link".equals(e.getActionCommand())) {
			TcCmd.setActiveCommand(new CmdLink());
		}
		else if ("move".equals(e.getActionCommand())) {
			TcCmd.setActiveCommand(new CmdMove());
		}
		else if ("merge".equals(e.getActionCommand())) {
			TcCmd.setActiveCommand(new CmdMerge());
		}
		else if ("delete".equals(e.getActionCommand())) {
			TcCmd.setActiveCommand(new CmdDelete());
		}
		else if ("edit".equals(e.getActionCommand())) {
			TcCmd.setActiveCommand(new CmdEdit());
		}
		else if ("add".equals(e.getActionCommand())) {
			TcCmd.setActiveCommand(new CmdAdd());
		}
		else if ("remove".equals(e.getActionCommand())) {
			TcCmd.setActiveCommand(new CmdRemove());
		}
		else if ("viewMove".equals(e.getActionCommand())) {
			TcCmd.setActiveCommand(new CmdViewMove());
		}
		else if ("viewZoom".equals(e.getActionCommand())) {
			TcCmd.setActiveCommand(new CmdViewZoom());
		}
		
		Component[] components = getComponents();
		for (int i = 0; i < components.length; i++ ) {
			if ( ( components[i] instanceof JButton ) == false ) continue;  
			JButton button = (JButton)components[i];
			if (button == null ) continue;
			button.setBackground(background);
			button.setFont(font);
		}
		
		JButton button = (JButton)e.getSource();
		if (button == null) return;
		mark(button);
		
		ViewRoot.updateActive();
	}
	
	public void mark(JButton button) {
		button.setFont(new Font(font.getName(), Font.BOLD, font.getSize()));		
	}
}
